package com.tianhui.consignortwofei.widget;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.RectF;
import android.text.TextUtils;
import android.util.AttributeSet;

import com.tianhui.consignortwofei.R;

/**
 * @author fgs
 * @date 2020/10/12
 * @description 弧形文字
 **/
public class ArcTextView extends androidx.appcompat.widget.AppCompatTextView {
    Paint mPaint = new Paint();
    private String text = "";

    public ArcTextView(Context context) {
        this(context, null);
    }

    public ArcTextView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public ArcTextView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

    }

    @Override
    protected void onDraw(Canvas canvas) {
        // TODO Auto-generated method stub
        super.onDraw(canvas);


        int srcWidth = getWidth();
        int srcHeight = getHeight() * 3;

        int unitHeight = srcHeight > srcWidth ? srcWidth / 10 : srcHeight / 10;
        int textSize = unitHeight;

        //换图区域，宽高自己设定，要圆要扁请随意
        RectF rectF = new RectF(0, 0, getWidth(), getHeight() * 3);
        //这个功能的重要点 其实是先画一个轨迹 然后再把文字画到这个轨迹上 所以 想要什么样的扭曲，就画什么样的轨迹，下面就是画一个轨迹
        Path path = new Path();
        //画一个弧形 也可以是椭圆  上面的矩形就是这个图形的区域大小了 后面两个参数记住 一个是起始位置 一个是所画角度而不是终点位置
        path.addArc(rectF, -180, 180);
        mPaint.setColor(getResources().getColor(R.color.colorGrayLight));
        //去锯齿的 不多说
        mPaint.setAntiAlias(true);
        mPaint.setTextSize(textSize);

        // 画布下移
        canvas.translate(0, unitHeight);
        // 绘制路径
        mPaint.setStyle(Paint.Style.STROKE);
//        canvas.drawPath(path, mPaint);

        // 下面就是绘制文本了
        mPaint.setStyle(Paint.Style.FILL);


        if (!TextUtils.isEmpty(text)) {
            float textWith = mPaint.measureText(text);
            float hOffset = (getWidth() - textWith) / 2 + unitHeight * 2;
            canvas.drawTextOnPath(text, path, hOffset, 20, mPaint);
        }

    }

    public void setText(String text) {
        this.text = text;
        invalidate();
    }
}
